package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.util.DBUtil;

import java.sql.*;

/**
 * 用于处理用户相关业务
 *
 * @author Chen
 */
@Controller
public class UserController {

    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户注册！");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        if (username == null || username.trim().isEmpty()
                || password == null || password.trim().isEmpty()
                || nickname == null || nickname.trim().isEmpty()
                || ageStr == null || ageStr.trim().isEmpty()
                || !ageStr.matches("[0-9]+")
        ) {
            response.sendRedirect("/reg_info_error.html");
            return;
        }

        int age = Integer.parseInt(ageStr);

        try (
                Connection connection = DBUtil.getConnection()
        ) {
            Statement statement = connection.createStatement();
            String queryName = "SELECT username FROM userinfo where username = ?";
            PreparedStatement preparedStatement1 = connection.prepareStatement(queryName);
            preparedStatement1.setString(1, username);
            if (preparedStatement1.executeQuery().next()) {
                response.sendRedirect("/have_user.html");
                return;
            }
            String sql = "INSERT INTO userinfo " +
                    "(username,password,nickname,age) " +
                    "VALUES " +
                    "(?, ?, ?, ?)";
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql);
            preparedStatement2.setString(1, username);
            preparedStatement2.setString(2, password);
            preparedStatement2.setString(3, nickname);
            preparedStatement2.setInt(4, age);
            int count = statement.executeUpdate(sql);
            if (count > 0) {
                response.sendRedirect("/reg_success.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理登录！");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            response.sendRedirect("/login_info_error.html");
            return;
        }

        try (
                Connection connection = DBUtil.getConnection()
        ) {
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM userinfo " +
                    "WHERE username = ? " +
                    "AND password= ? ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                response.sendRedirect("/login_success.html");
            } else {
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
